#include <QtGui>
#include "t2cDialog.h"

T2CDialog::T2CDialog(QWidget *parent) :
    QWidget(parent),
    iSZ_valid( false ),
    manager( new T2CManager() )
{
    lbl_host  = new QLabel( "SCADA ip:" );
    lbl_port  = new QLabel( "Port:" );
    lbl_cmd   = new QLabel( "Command:" );
    lbl_i_SZ  = new QLabel( "i_SZ of object:" );

    lnedt_host = new QLineEdit( "10.0.0.39" );
    lnedt_port = new QLineEdit( "47001" );
    lnedt_cmd  = new QLineEdit( "" );
    inedt_i_SZ = new QLineEdit( "" );

    btn_connect     = new QPushButton( "Connect to SCADA" );
    btn_disconnect  = new QPushButton( "Disconnect SCADA" );
    btn_exec        = new QPushButton( "Execute" );
    btn_Subscribe   = new QPushButton( "Subscribe" );
    btn_Unsubscribe = new QPushButton( "Unsubscribe" );

    lvLog = new QListView;
    logModel.setStringList( QStringList() );
    lvLog->setModel( &logModel );


    //Layout settings
    grdLayout = new QGridLayout;
    grdLayout->setSpacing( 1 );
    grdLayout->setMargin( 1 );
    grdLayout->addWidget( lbl_host,    0, 0 );
    grdLayout->addWidget( lnedt_host,  0, 1 );
    grdLayout->addWidget( lbl_port,    0, 2 );
    grdLayout->addWidget( lnedt_port,  0, 3 );
    grdLayout->addWidget( btn_connect, 0, 4 );
    grdLayout->addWidget( btn_disconnect, 0, 5 );

    grdLayout->addWidget( lbl_i_SZ,          1, 0 );
    grdLayout->addWidget( inedt_i_SZ,      1, 1, 1, 3 );
    grdLayout->addWidget( btn_Subscribe,   1, 4 );
    grdLayout->addWidget( btn_Unsubscribe, 1, 5 );

    grdLayout->addWidget( lbl_cmd,   2, 0 );
    grdLayout->addWidget( lnedt_cmd, 2, 1, 1, 3 );
    grdLayout->addWidget( btn_exec,  2, 4 );

    grdLayout->addWidget( lvLog, 3, 0, 1, 6 );


    setLayout( grdLayout );

    connect( btn_connect, SIGNAL(clicked()), SLOT(slotConnect()) );
    connect( btn_disconnect, SIGNAL(clicked()), SLOT(slotDisconnect()) );
    connect( btn_exec, SIGNAL(clicked()), SLOT(slotExec()) );
    connect( btn_Subscribe, SIGNAL( clicked() ), this, SLOT( slotSubscribe() ) );
    connect( btn_Unsubscribe, SIGNAL( clicked() ), this, SLOT( slotUnsubscribe() ) );
    connect( manager, SIGNAL( sgnValueRecieved(QString) ), this, SLOT( sltValueReceived(QString) ));


    btn_connect->setEnabled( true );
    btn_disconnect->setEnabled( false );
    btn_exec->setEnabled( false );
    btn_Subscribe->setEnabled( false );
    btn_Unsubscribe->setEnabled( false );
}

//-------------------------------------

void T2CDialog::slotConnect()
{
    host = /*QHostAddress*/( lnedt_host->text() );
    port = lnedt_port->text().toInt();

    manager->Connect( host, port );

    btn_connect->setEnabled( false );
    btn_disconnect->setEnabled( true );
    btn_exec->setEnabled( true );
    btn_Subscribe->setEnabled( true );
    btn_Unsubscribe->setEnabled( false );
}

//-------------------------------------

void T2CDialog::slotDisconnect()
{
    manager->Disconnect();

    btn_connect->setEnabled( true );
    btn_disconnect->setEnabled( false );
    btn_exec->setEnabled( false );
    btn_Subscribe->setEnabled( false );
    btn_Unsubscribe->setEnabled( false );
}

//-------------------------------------

void T2CDialog::slotExec()
{
    if( manager )
    {
        //QTime t;
        //t.start();
        //lvLog->append( "===> Started at:" + QTime::currentTime().toString( "hh:mm:ss.zzz" ) );
        manager->ExecCmd( lnedt_cmd->text() );
        //manager->getVariable( 782522 );
        //manager->waitForReady();
        //manager->probeVariable( 782522 );
        //lvLog->append( "===> Stopped at:" + QTime::currentTime().toString( "hh:mm:ss.zzz" ) );
        //lvLog->append( QString( "===>   Time elapsed: %1 ms" ).arg( t.elapsed() ) );
    }

}

//-------------------------------------

void T2CDialog::slotSubscribe()
{
    check_iSZ();
    if( iSZ_valid )
    {
        manager->subscribe( iSZ );
        btn_Subscribe->setEnabled( false );
        btn_Unsubscribe->setEnabled( true );
    }
}

//-------------------------------------

void T2CDialog::slotUnsubscribe()
{
    check_iSZ();
    if( iSZ_valid )
    {
        manager->unsubscribe( iSZ );
        btn_Subscribe->setEnabled( true );
        btn_Unsubscribe->setEnabled( false );
    }
}

//-------------------------------------

void T2CDialog::check_iSZ()
{
    QString i_SZ = inedt_i_SZ->text();

    if( i_SZ.length()  )
    {
        QRegExp reg( "[0-9]+" );
        QRegExpValidator v( reg, 0 );
        int pos = 0;
        if( v.validate( i_SZ, pos ) == QValidator::Acceptable )
        {
            iSZ = i_SZ.toLongLong();
            iSZ_valid = true;
        }
        else
        {
            iSZ_valid = false;
        }
    }
}

//-------------------------------------

void T2CDialog::sltValueReceived( QString t )
{
    //lvLog->append( t );
    logModel.insertRows( logModel.rowCount(), 1 );
    logModel.setData( logModel.index( logModel.rowCount()-1 ), t );
}





